package control;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.StringTokenizer;

/** Infos sur une station
 * @author Josselyn Hermitte
 * @author Yanis Lisima
 *
 */
public class InfoHost {
	
	/** */
	public static final int TIMEOUT= 3000;
	
	private String hostname;
	private String hostaddr;
	private boolean reachable;
	private String cpuusage;
	private String mem;
	
	/** Instancier une nouvelle classe 
	 * @param hostname
	 * @param hostaddr
	 */
	public InfoHost(String hostname, String hostaddr) {
		this.hostname = hostname;
		this.hostaddr = hostaddr;
		this.reachable = false;
		this.cpuusage = null;
		this.mem = null;
	}

	/**
	 * @return the hostname
	 */
	public String getHostname() {
		return hostname;
	}

	/**
	 * @param hostname the hostname to set
	 */
	public void setHostname(String hostname) {
		this.hostname = hostname;
	}

	/**
	 * @return the hostaddr
	 */
	public String getHostaddr() {
		return hostaddr;
	}

	/**
	 * @param hostaddr the hostaddr to set
	 */
	public void setHostaddr(String hostaddr) {
		this.hostaddr = hostaddr;
	}

	/**
	 * @return the reachable
	 */
	public boolean isReachable() {
		return reachable;
	}

	/**
	 * @param reachable the reachable to set
	 */
	public void setReachable(boolean reachable) {
		this.reachable = reachable;
	}

	/**
	 * @return the cpuusage
	 */
	public String getCpuusage() {
		return cpuusage;
	}

	/**
	 * @param cpuusage the cpuusage to set
	 */
	public void setCpuusage(String cpuusage) {
		this.cpuusage = cpuusage;
	}

	/**
	 * @return the mem
	 */
	public String getMem() {
		return mem;
	}

	/**
	 * @param mem the mem to set
	 */
	public void setMem(String mem) {
		this.mem = mem;
	}
	
	/**
	 * @param username
	 * @param password
	 */
	public void getInfo(String username, String password){
		try {
			reachable= InetAddress.getByName(hostaddr).isReachable(TIMEOUT);
		} catch (UnknownHostException e) {
			System.out.println(e);
		} catch (IOException e) {
			System.out.println(e);
		}
		ClientSSH ssh= new ClientSSH();
		String sortie= null;
		if(ssh.connect(hostaddr, username, password)){
			sortie= ssh.executeCommand(
					"free -m"
							//"top -n 1 | awk 'NR == 3, NR == 4'"
					);
			ssh.disconnect();
			StringTokenizer args= new StringTokenizer(sortie);
			while(args.hasMoreTokens() && !args.nextToken().equals("em:"));
			this.mem= args.nextToken();
			this.mem= args.nextToken() + "/" + this.mem;
		}
		/*
		if(ssh.connect(hostaddr, username, password)){
			sortie= ssh.executeCommand(
					"topas"
					//"top -n 1 | awk 'NR == 3, NR == 4'"
			);
			ssh.disconnect();
			System.out.println(sortie);
			//StringTokenizer args= new StringTokenizer(sortie);
			//while(args.hasMoreTokens() && !args.nextToken().equals("em:"));
			//this.mem= args.nextToken();
			//this.mem= args.nextToken() + "/" + this.mem;
		}*/
	}
	
	@Override
	public String toString() {
		return String.format("InfoHost [%s, %s, %s, %s]", hostname, reachable,
				cpuusage, mem);
	}

}
